ec.pkg("rush");
rush.account = {
    isLogin: function () {
        return ec.account.isLogin()
    },
    getUid: function () {
        return ec.account.uid
    }
};
rush.sbom = {
    getSbom: function (a) {
        return ec.product.getSkuInfo(a)
    },
    isRushProduct: function () {
        return $("#pro-global-parameter").attr("data-skulist-rushbuy") ? true : false
    },
    isRushSbom: function (a) {
        var b = $("#pro-global-parameter").attr("data-skulist-rushbuy");
        var d = "," + b + ",",
            c = "," + a + ",";
        if (d.indexOf(c) != -1) {
            return true
        }
        return false
    },
    getCurrSbom: function () {
        return ec.product.getSkuInfo(ec.product.getSku())
    },
    getCurrSkuId: function () {
        return ec.product.getSku()
    }
};
rush.activity = {
    _loadStatus: 0,
    _activityMap: {},
    getActivity: function (a) {
        return rush.activity._activityMap[a]
    },
    isLoaded: function () {
        return rush.activity._loadStatus == 1 ? true : false
    },
    afterLoaded: function (c) {
        if (c.sbom.id != rush.business.runningSkuId) {
            return
        }
        btnStatus = rush.business.newBtnStatus("A3U3Q3S3", c.sbom.id);
        rush.business.displayBtnStatus(c.sbom, null, btnStatus);
        if (rush.activity.isLoaded()) {
            c.doCallBack(c.sbom)
        } else {
            var b = $("#pro-global-parameter").attr("data-skulist-rushbuy");
            var a = domainRush + "/getSkuRushbuyInfo.json?skuIds=" + b + "&t=" + new Date().getTime();
            $.ajax({
                url: a,
                dataType: "jsonp",
                timeout: 5000,
                success: function (g) {
                    if (g) {
                        rush.business.setOffsetTime(new Date().getTime(), g.currentTime);
                        if (g.success == true && g.skuRushBuyInfoList) {
                            var e = g.skuRushBuyInfoList;
                            for (var f = 0; f < e.length; f++) {
                                if (e[f] && e[f].skuId) {
                                    rush.activity._activityMap[e[f].skuId] = e[f];
                                    if (e[f].startTime && e[f].endTime) {
                                        rush.activity._activityMap[e[f].skuId].startTime = new Date(e[f].startTime).format("yyyy-MM-dd HH:mm:ss");
                                        rush.activity._activityMap[e[f].skuId].endTime = new Date(e[f].endTime).format("yyyy-MM-dd HH:mm:ss")
                                    }
                                }
                            }
                            rush.activity._loadStatus = 1
                        }
                    } else {
                        var d = '<a onclick="rush.business.clickBtn()" class="button-book-2 button-style-1" ><span>立即申购</span></a>';
                        $("#pro-operation").html(d);
                        $("#pro-operation").css("visibility", "visible")
                    }
                },
                complete: function (f, e) {
                    if (e == "timeout") {
                        var d = '<a onclick="rush.business.clickBtn()" class="button-book-2 button-style-1" ><span>立即申购</span></a>';
                        $("#pro-operation").html(d);
                        $("#pro-operation").css("visibility", "visible")
                    } else {
                        c.doCallBack(c.sbom)
                    }
                }
            })
        }
    }
};
rush.qualification = {
    afterChecked: function (c) {
        if (c.sbom.id != rush.business.runningSkuId) {
            return
        }
        var e, d;
        var a = c.act;
        if (!a || !a.isRushBuySku) {
            return
        }
        if (!rush.account.isLogin()) {
            e = 0;
            c.doCallBack(c.sbom, c.act, e);
            return
        }
        activityId = a.activityId;
        uid = rush.account.getUid();
        e = ec.util.cookie.get("isqueue-" + activityId + "-" + uid);
        d = ec.util.cookie.get("queueSign-" + activityId + "-" + uid);
        if (e && d) {
            c.doCallBack(c.sbom, c.act, e)
        } else {
            e = 0;
            c.doCallBack(c.sbom, c.act, e);
            var f = a.qids ? a.qids : "0";
            var b = domainYY + "/ivy/isqueue.jsp?uid=" + uid + "&qid=" + f + "&t=" + new Date().getTime();
            $.ajax({
                url: b,
                dataType: "jsonp",
                timeout: 10000,
                jsonpCallback: "callbackqueue",
                success: function (g) {
                    if (g) {
                        if (g.isqueue && g.queueSign) {
                            var h = {
                                expires: a.endTime,
                                domain: ".vmall.com"
                            };
                            e = g.isqueue;
                            d = g.queueSign;
                            if (e && d) {
                                ec.util.cookie.set("isqueue-" + activityId + "-" + uid, e, h);
                                ec.util.cookie.set("queueSign-" + activityId + "-" + uid, d, h);
                                e = ec.util.cookie.get("isqueue-" + activityId + "-" + uid);
                                d = ec.util.cookie.get("queueSign-" + activityId + "-" + uid);
                                c.doCallBack(c.sbom, c.act, e)
                            }
                        }
                    }
                }
            })
        }
    }
};
rush.business = {
    offsetTime: 0,
    setOffsetTime: function (b, a) {
        rush.business.offsetTime = a - b
    },
    getSysDate: function () {
        return new Date(new Date().getTime() + rush.business.offsetTime)
    },
    timer: null,
    btnStatus: {},
    btnSt2Mode: {
        AXUXQXSX: 0,
        A0UXQXSX: 1,
        AXUXQXS2: 2,
        A3UXQXS1: 3,
        A1U1Q0S1: 4,
        A1U1Q1S1: 4,
        A1U1Q2S1: 5,
        A2U1Q2S1: 5,
        A1U0QXS1: 6,
        A2U1Q0S1: 7,
        A2U0QXS1: 7,
        A2U1Q1S1: 7,
        A3U3Q3S3: 8,
        A4U1Q0S1: 9,
    },
    newBtnStatus: function (b, a) {
        var c = {};
        c.btnStatus = b;
        c.btnMode = rush.business.btnSt2Mode[b];
        c.skuId = a;
        return c
    },
    runningSkuId: null,
    doGoLogin: function () {
        var a = domainMain + "/account/login?url=" + encodeURIComponent(window.location.pathname + "#" + rush.sbom.getCurrSkuId()) + "&_t=" + new Date().getTime();
        window.location.href = a
    },
    doGoRush: function () {
        var c = rush.sbom.getCurrSbom();
        var f = {};
        f.mainSku = c.id;
        f.targetUrl = c.gotoUrl;
        f.backUrl = domainMain + window.location.pathname + "#" + c.id;
        var b = $("#extendSelect").attr("skuid");
        var d = $("#accidentSelect").attr("skuid");
        var e = [];
        if (b) {
            e.push(b)
        }
        if (d) {
            e.push(d)
        }
        f.accessoriesSkus = e.join(",");
        var a = "";
        if (f.targetUrl && f.mainSku && f.mainSku.length > 0) {
            a = f.targetUrl + "?mainSku=" + f.mainSku;
            if (f.accessoriesSkus && f.accessoriesSkus.length > 0) {
                a += "&accessoriesSkus=" + f.accessoriesSkus
            }
            if (f.backUrl && f.backUrl.length > 0) {
                a += "&backUrl=" + encodeURIComponent(f.backUrl) + ""
            }
            a += "&_t=" + new Date().getTime();
            window.location.href = a
        } else {
            return
        }
    },
    clickBtn: function () {
        if (rush.account.isLogin()) {
            rush.business.doGoRush()
        } else {
            rush.business.doGoLogin()
        }
    },
    displayBtnStatus: function (e, f, d) {
        if (e.id != rush.business.runningSkuId) {
            return
        }
        if (rush.business.btnStatus && rush.business.btnStatus.btnMode == d.btnMode) {
            return
        }
        rush.business.btnStatus = d;
        if (rush.business.timer) {
            clearInterval(rush.business.timer);
            rush.business.timer = null
        }
        $("#pro-operation").html("");
        $("#pro-operation-msg").html("");
        var a = "";
        switch (d.btnMode) {
            case 0:
                a += '<a class="product-button02 disabled"><span>暂时缺货</span></a>';
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            case 1:
                if (ec.product.isEasyBuy()) {
                    $("#pro-quantity-area").hide();
                    $("#pro-quantity-area-nochange").show();
                    a += '<a onclick="ec.product.orderEasyBuy()" class="product-button02" ><span>优享购</span></a>'
                } else {
                    a += '<a class="product-button02 disabled"><span>暂时缺货</span></a>'
                }
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            case 2:
                if (ec.product.isEasyBuy()) {
                    $("#pro-quantity-area").hide();
                    $("#pro-quantity-area-nochange").show();
                    a += '<a onclick="ec.product.orderEasyBuy()" class="product-button02" ><span>优享购</span></a>'
                } else {
                    a += '<a class="product-button02 disabled"><span>已售完</span></a>'
                }
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            case 3:
                a += '<a class="product-button02 disabled"><span>暂时缺货</span></a>';
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            case 4:
                if (ec.product.isEasyBuy()) {
                    $("#pro-quantity-area").hide();
                    $("#pro-quantity-area-nochange").show();
                    a += '<a onclick="ec.product.orderEasyBuy()" class="product-button01" ><span>优享购</span></a>'
                }
                a += '<a class="product-button02 disabled">即将开始</a></div><div id="pro-operation-countdown" class="product-time"></div>';
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            case 5:
                a = '<i></i><div class="tips-text"><p><span>温馨提示：</span><span id="pro-msg-title">您不符合本次购买条件。</span></p></div>';
                a = '<div class="tips-style-2 tips-area" style="width:100%">' + a + "</div>";
                $("#product-tips").html("温馨提示：您不符合本次购买条件。");
                $("#product-tips").show();
                $("#product-operation").css("visibility", "hidden");
                break;
            case 6:
                a = '<a onclick="rush.business.clickBtn()" class="product-button02" ><span>提前登录</span></a><div id="pro-operation-countdown" class="product-time"></div>';
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            case 7:
                a = '<a onclick="rush.business.clickBtn()" class="product-button02" ><span>立即申购</span></a>';
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            case 8:
                a = '<a class="product-button02 disabled"><span>正在加载...</span></a>';
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            case 9:
                a = '<a class="product-button02 disabled" ><span>立即申购</span></a> <span class="product-tips01">还有机会，有用户未完成订单，15分钟内会被取消</span>';
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break;
            default:
                a += '<a class="product-button02 disabled"><span>暂时缺货</span></a>';
                $("#pro-operation").html(a);
                $("#pro-operation").css("visibility", "visible");
                $("#product-operation").show();
                break
        }
        if ((d.btnMode == 4 || d.btnMode == 6) && $("#pro-operation-countdown") && f && f.startTime) {
            var g = f.startTime.parseDate("yyyy-MM-dd HH:mm:ss").format("MM月dd日 HH:mm");
            var c = rush.business.getSysDate();
            ec.ui.countdown2($("#pro-operation-countdown"), {
                html: "<p>" + g + "开售:	</p><ul><li><span>{#day}</span></li><li><em>天</em></li><li><span>{#hours}</span></li><li><em>:</em></li><li><span>{#minutes}</span></li><li><em>:</em></li><li><span>{#seconds}</span></li>",
                now: c,
                endTime: f.startTime,
                callback: function (b) {
                    d = rush.business.newBtnStatus("A2U1Q1S1", e.id);
                    rush.business.displayBtnStatus(e, act, d)
                }
            });
            rush.business.timer = $($("#pro-operation-countdown")).data("countdown");
            if (!rush.business.timer) {
                d = rush.business.newBtnStatus("A2U1Q1S1", e.id);
                rush.business.displayBtnStatus(e, act, d)
            }
        }
    },
    doIfSbomChanged: function () {
        var a = rush.sbom.getCurrSkuId();
        rush.business.runningSkuId = a;
        rush.business.btnStatus = null;
        if (ec.product.productType == 4) {
            var d = rush.sbom.getSbom(a);
            $("#pro-operation").html('<a target="_blank" href="' + d.gotoUrl + '" class="product-button02" ><span>参加抢购</span></a>');
            $("#pro-operation").css("visibility", "visible");
            $("#product-operation").show();
            return
        }
        if (!rush.sbom.isRushProduct() || !rush.sbom.isRushSbom(a)) {
            return
        }
        if (rush.sbom.isRushProduct() && rush.sbom.isRushSbom(a)) {
            $("#pro-quantity-area").hide();
            $("#pro-quantity-area-nochange").show()
        }
        var b = null,
            c = null;
        b = rush.sbom.getSbom(a);
        if (rush.sbom.isRushProduct()) {
            rush.activity.afterLoaded({
                sbom: b,
                doCallBack: function (f) {
                    if (f.id != rush.business.runningSkuId) {
                        return
                    }
                    var e = f.id;
                    if (rush.sbom.isRushSbom(e)) {
                        act = rush.activity.getActivity(e);
                        if (f && act && act.isRushBuySku) {
                            rush.qualification.afterChecked({
                                sbom: f,
                                act: act,
                                doCallBack: function (i, g, j) {
                                    if (i.id != rush.business.runningSkuId) {
                                        return
                                    }
                                    var h = rush.business.calculateBtnStatus(i, g, j);
                                    rush.business.displayBtnStatus(i, g, h)
                                }
                            })
                        } else {
                            btnStatus = rush.business.newBtnStatus("A0UXQXSX", f.id);
                            rush.business.displayBtnStatus(f, act, btnStatus);
                            return
                        }
                    } else {
                        return
                    }
                }
            })
        } else {
            return
        }
    },
    calculateBtnStatus: function (d, a, h) {
        var c = "AXUXQXSX";
        var g = new Date(),
            e = new Date(),
            b = new Date();
        try {
            g = rush.business.getSysDate();
            e.setTime(a.startTime.parseDate("yyyy-MM-dd HH:mm:ss"));
            b.setTime(a.endTime.parseDate("yyyy-MM-dd HH:mm:ss"))
        } catch (f) {
            c = "AXUXQXSX";
            return rush.business.newBtnStatus(c, d.id)
        }
        if (g > b) {
            if (a.skuStatus == 0) {
                c = "AXUXQXS2";
                return rush.business.newBtnStatus(c, d.id)
            }
            c = "A3UXQXS1";
            return rush.business.newBtnStatus(c, d.id)
        }
        if (!a.qids || a.qids == "0") {
            result = 1
        } else {
            if (h == 2) {
                result = 2
            } else {
                if (h == 1) {
                    result = 1
                } else {
                    result = 0
                }
            }
        }
        if (g < e) {
            if (rush.account.isLogin()) {
                switch (result) {
                    case 1:
                        c = "A1U1Q1S1";
                        break;
                    case 2:
                        c = "A1U1Q2S1";
                        break;
                    default:
                        c = "A1U1Q0S1"
                }
            } else {
                c = "A1U0QXS1"
            }
            return rush.business.newBtnStatus(c, d.id)
        }
        if (g >= e) {
            if (a.skuStatus == 0) {
                c = "AXUXQXS2";
                return rush.business.newBtnStatus(c, d.id)
            } else {
                if (a.skuStatus == 2) {
                    c = "A4U1Q0S1";
                    return rush.business.newBtnStatus(c, d.id)
                }
            }
            if (rush.account.isLogin()) {
                switch (result) {
                    case 1:
                        c = "A2U1Q1S1";
                        break;
                    case 2:
                        c = "A2U1Q2S1";
                        break;
                    default:
                        c = "A2U1Q0S1"
                }
            } else {
                c = "A2U0QXS1"
            }
            return rush.business.newBtnStatus(c, d.id)
        }
    }
};
